package com.beiing.tianshuai.tianshuai.widget;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import java.util.Date;
import java.util.Locale;

import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

/**
 * Created by magikliu on 2017/9/4.
 */

public class MyRefreshHeader extends RelativeLayout implements RefreshHeader {

    public static String REFRESH_HEADER_PULLDOWN = "下拉开始刷新";
    public static String REFRESH_HEADER_REFRESHING = "正在刷新...";
    public static String REFRESH_HEADER_RELEASE = "释放立即刷新";
    public static String REFRESH_HEADER_FINISH = "刷新成功";
    public static String REFRESH_HEADER_FAILED = "刷新失败";
    public static String REFRESH_HEADER_LASTTIME = "上次更新 M-d HH:mm";

    private TextView mHeaderText;                       //标题文本
    private TextView mLastUpdateText;
    private ImageView mProgressView;                    //刷新动画视图
    private AnimationDrawable mProgressDrawable;        //刷新动画
    private java.text.DateFormat mFormat = new java.text.SimpleDateFormat("上次更新 M-d HH:mm", Locale.CHINA);
    private ImageView mascot_left;                      //吉祥物图片 左视
    private ImageView mascot_right;                     //吉祥物图片 右视
    protected RefreshKernel mRefreshKernel;

    public MyRefreshHeader(Context context) {
        super(context);
        this.initView(context, null, 0);
    }

    public MyRefreshHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.initView(context, attrs, 0);
    }

    public MyRefreshHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initView(context, attrs, defStyleAttr);
    }

    private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        DensityUtil density = new DensityUtil();

        setMinimumHeight(density.dip2px(80));

        mProgressDrawable = new AnimationDrawable();
        Drawable drawable = getResources().getDrawable(R.mipmap.puppy_c_left);
        Drawable drawable1 = getResources().getDrawable(R.mipmap.puppy_c_right);
        mProgressDrawable.addFrame(drawable, 200);
        mProgressDrawable.addFrame(drawable1, 200);
        mProgressView = new ImageView(context);
        mProgressView.setBackgroundDrawable(mProgressDrawable);
        LayoutParams lpProgress = new LayoutParams(density.dip2px(30), density.dip2px(30));
        lpProgress.leftMargin = density.dip2px(90);    //位置问题
        lpProgress.addRule(CENTER_VERTICAL);
        lpProgress.addRule(ALIGN_PARENT_LEFT);
        addView(mProgressView, lpProgress);

        mascot_left = new ImageView(context);
        mascot_left.setImageResource(R.mipmap.puppy_c_left);
        addView(mascot_left, lpProgress);

        mascot_right = new ImageView(context);
        mascot_right.setImageResource(R.mipmap.puppy_c_right);
        addView(mascot_right, lpProgress);

        LinearLayout layout = new LinearLayout(context, attrs, defStyleAttr);
        layout.setGravity(Gravity.CENTER_HORIZONTAL);
        layout.setOrientation(LinearLayout.VERTICAL);
        mHeaderText = new TextView(context);
        mHeaderText.setText(REFRESH_HEADER_PULLDOWN);
        mHeaderText.setTextColor(0xff666666);
        mHeaderText.setTextSize(16);

        mLastUpdateText = new TextView(context);
        mLastUpdateText.setText(mFormat.format(new Date()));
        mLastUpdateText.setTextColor(0xff7c7c7c);
        mLastUpdateText.setTextSize(12);
        LinearLayout.LayoutParams lpHeaderText = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
        lpHeaderText.leftMargin = density.dip2px(20);
        lpHeaderText.rightMargin = density.dip2px(20);
        layout.addView(mHeaderText, lpHeaderText);
        LinearLayout.LayoutParams lpUpdateText = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
        layout.addView(mLastUpdateText, lpUpdateText);

        LayoutParams lpHeaderLayout = new LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
        lpHeaderLayout.addRule(CENTER_IN_PARENT);
        addView(layout, lpHeaderLayout);
    }

    @Override
    public void onPullingDown(float percent, int offset, int headerHeight, int extendHeight) {

    }

    @Override
    public void onReleasing(float percent, int offset, int headerHeight, int extendHeight) {

    }

    @NonNull
    @Override
    public View getView() {
        //真实的视图就是自己，不能返回null
        return this;
    }

    @Override
    public SpinnerStyle getSpinnerStyle() {
        //指定为平移，不能null
        return SpinnerStyle.Translate;
    }

    @Override
    public void setPrimaryColors(int... colors) {

    }

    @Override
    public void onInitialized(RefreshKernel kernel, int height, int extendHeight) {

    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public void onStartAnimator(RefreshLayout layout, int height, int extendHeight) {
        mProgressDrawable.start();              //开始动画
    }

    @Override
    public int onFinish(RefreshLayout layout, boolean success) {
        Log.i("onFinish", success + "");
        mProgressDrawable.stop();
        if (success) {
            mHeaderText.setText(REFRESH_HEADER_FINISH);
            mLastUpdateText.setText(mFormat.format(new Date()));
        } else {
            mHeaderText.setText(REFRESH_HEADER_FAILED);
        }
        return 500;     //延迟500毫秒之后再弹回
    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
        switch (newState) {
            case None:
                break;
            case PullDownToRefresh:
                mascot_left.setVisibility(VISIBLE);
                mascot_right.setVisibility(GONE);
                mProgressView.setVisibility(GONE);
                mHeaderText.setText(REFRESH_HEADER_PULLDOWN);
                break;
            case Refreshing:
                mProgressView.setVisibility(VISIBLE);
                mascot_left.setVisibility(GONE);
                mascot_right.setVisibility(GONE);
                mHeaderText.setText(REFRESH_HEADER_REFRESHING);
                break;
            case ReleaseToRefresh:
                mascot_right.setVisibility(VISIBLE);
                mascot_left.setVisibility(GONE);
                mHeaderText.setText(REFRESH_HEADER_RELEASE);
                break;
        }
    }

}
